/* $RCSFile: StringLocalizerDB.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice/PageModules/StringLocalizer/StringLocalizerDB.cs $
 * $log$
 * Revision 19 2010/08/09 09:44:00  christian.surieux
 *   Added Log to cs sources files
 * Revision 6 2010/08/06 19:56:26  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;

using AddonNice;
using AddonNice.Configuration;
using AddonNice.Diagnostics;
using AddonNice.Settings;

namespace AddonNice.PageModules.StringLocalizer
{
    public class StringLocalizerDB : DBDataStore
    {
		public SqlDataReader GetAllStringResourceByType(string @resourceType) 
        {
            SqlCommand myCommand				=	new SqlCommand("dbo.adn_GetAllStringResourceByType", myConnection);
            myCommand.CommandType				=	CommandType.StoredProcedure;

			SqlParameter parameterresourceType  =	new SqlParameter("@resourceType", SqlDbType.NVarChar, 256);
			parameterresourceType.Value			=	resourceType;
			myCommand.Parameters.Add(parameterresourceType);

			SqlDataReader result				=	null;
			try
			{
				myConnection.Open();
				result							=   myCommand.ExecuteReader(CommandBehavior.CloseConnection);

			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"StringLocalizerDB GetAllStringResourceByType ex: {0}",exp),DBTraceSwitch.Sw.Info);
				if ( result != null )
					result.Close();
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
				throw;
			}
			return result;
		}
        
		public SqlDataReader GetAllStringResourceByCountry(string @resourceType,string cultureCode) 
        {
            SqlCommand myCommand				=	new SqlCommand("dbo.adn_GetAllStringResourceByCountry", myConnection);
            myCommand.CommandType				=	CommandType.StoredProcedure;

			SqlParameter parameterresourceType  =	new SqlParameter("@resourceType", SqlDbType.NVarChar, 256);
			parameterresourceType.Value			=	resourceType;
			myCommand.Parameters.Add(parameterresourceType);

			SqlParameter parametercultureCode   =	new SqlParameter("@cultureCode", SqlDbType.NVarChar, 10);
			parametercultureCode.Value			=	cultureCode;
			myCommand.Parameters.Add(parametercultureCode);

			SqlDataReader result				=	null;
			try
			{
				myConnection.Open();
				result							=   myCommand.ExecuteReader(CommandBehavior.CloseConnection);

			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"StringLocalizerDB GetAllStringResourceByCountry ex: {0}",exp),DBTraceSwitch.Sw.Info);
				if ( result != null )
					result.Close();
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
				throw;
			}
			return result;
		}

        public string GetStringResource(string resourceType,string cultureCode,string resourceKey)
        {
            SqlCommand myCommand				=	new SqlCommand("dbo.adn_GetStringResource", myConnection);
            myCommand.CommandType				=	CommandType.StoredProcedure;

			SqlParameter parameterresourceType  =	new SqlParameter("@resourceType", SqlDbType.NVarChar, 256);
			parameterresourceType.Value			=	resourceType;
			myCommand.Parameters.Add(parameterresourceType);

			SqlParameter parametercultureCode   =	new SqlParameter("@cultureCode", SqlDbType.NVarChar, 10);
			parametercultureCode.Value			=	cultureCode;
			myCommand.Parameters.Add(parametercultureCode);

			SqlParameter parameterresourceKey   =	new SqlParameter("@resourceKey", SqlDbType.NVarChar, 128);
			parameterresourceKey.Value			=	resourceKey;
			myCommand.Parameters.Add(parameterresourceKey);

            string value                        =   string.Empty;
			SqlDataReader result				=	null;
			try
			{
				myConnection.Open();
				result							=   myCommand.ExecuteReader(CommandBehavior.CloseConnection);
                if ( result.Read() )
                    value                       =   (string)result["resourceValue"];

			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"StringLocalizerDB GetStringResource ex: {0}",exp),DBTraceSwitch.Sw.Info);
				throw;
			}
            finally
            {
				if ( result != null )
					result.Close();
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
            }
			return value;
        }
        

		public void AddStringResource(string resourceType,string cultureCode,string resourceKey,string resourceValue) 
		{
            SqlCommand myCommand				    =	new SqlCommand("dbo.adn_AddStringResource", myConnection);
            myCommand.CommandType				    =	CommandType.StoredProcedure;

			SqlParameter parameterresourceType      =	new SqlParameter("@resourceType", SqlDbType.NVarChar, 256);
			parameterresourceType.Value			    =	resourceType;
			myCommand.Parameters.Add(parameterresourceType);

			SqlParameter parametercultureCode       =	new SqlParameter("@cultureCode", SqlDbType.NVarChar, 10);
			parametercultureCode.Value			    =	cultureCode;
			myCommand.Parameters.Add(parametercultureCode);

			SqlParameter parameterresourceKey       =	new SqlParameter("@resourceKey", SqlDbType.NVarChar, 128);
			parameterresourceKey.Value			    =	resourceKey;
			myCommand.Parameters.Add(parameterresourceKey);

			SqlParameter parameterresourceValue     =	new SqlParameter("@resourceValue", SqlDbType.NVarChar, 4000);
			parameterresourceValue.Value            =	resourceValue;
			myCommand.Parameters.Add(parameterresourceValue);

            try
			{
				myConnection.Open();
				myCommand.ExecuteNonQuery();
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]StringLocalizerDB AddStringResource ex: {0}",exp),DBTraceSwitch.Sw.Info);
				throw;
			}
			finally
			{
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
			}
        }

    }
}

